package com.sanpu.kxe.business.main.fragment;

import android.support.v4.view.ViewPager;
import android.view.View;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseFragment;
import com.sanpu.kxe.business.main.adapter.NewsPagerAdapter;
import com.sanpu.kxe.business.main.bean.NewsColumnsOutput;
import com.sanpu.kxe.business.main.contract.NewsContract;
import com.sanpu.kxe.business.main.presenter.NewsPresenter;
import com.sanpu.kxe.common.view.CommonTitleView;
import com.sanpu.kxe.common.view.SlideTabView;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：资讯
 * Created by 薛勇军 on 2018/8/28.
 */

public class NewsFragment extends BaseFragment implements NewsContract.View {

    @Bind(R.id.commonTitle)
    CommonTitleView commonTitleView;
    @Bind(R.id.tabView)
    SlideTabView vTabView;
    @Bind(R.id.view_page)
    ViewPager vViewPage;

    private NewsPagerAdapter vFragmentPagerAdapter;

    private ArrayList<String> mTabEntities = new ArrayList<>();

    private NewsContract.Presenter mPresenter;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisible()) {
            ImmersionBar mImmersionBar = ImmersionBar.with(this);
            mImmersionBar.statusBarColorTransformEnable(true)
                    .statusBarDarkFont(true, 0.5f)
                    .statusBarView(ButterKnife.findById(mRootView, R.id.top_view3))
                    .init();
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_news;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitleView.setTitle("资讯");
        commonTitleView.hiddenLeftView();

        mPresenter = new NewsPresenter(this);
        mPresenter.getNewsColumns();
    }

    @Override
    public void showNewsColumnsView(List<NewsColumnsOutput> columnsOutputs) {
        if (columnsOutputs != null && !columnsOutputs.isEmpty()) {
            if (vFragmentPagerAdapter == null) {
                for (NewsColumnsOutput retDatBean : columnsOutputs) {
                    mTabEntities.add(retDatBean.getColumnName());
                }
                vFragmentPagerAdapter = new NewsPagerAdapter(getChildFragmentManager(),
                        columnsOutputs);
                vViewPage.setOffscreenPageLimit(columnsOutputs.size() - 1);
                vViewPage.setAdapter(vFragmentPagerAdapter);
                vViewPage.setCurrentItem(0);
                vTabView.setVisibility(View.VISIBLE);
                vTabView.initTab(mTabEntities, vViewPage);
            }
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(NewsContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
